package com.hdyg.sixg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.home.MiniShopActivity;
import com.hdyg.sixg.activity.trans.MiningActivity;
import com.hdyg.sixg.adapter.MyMiniAdapter;
import com.hdyg.sixg.entry.MiniBean;
import com.hdyg.sixg.mvp.Contrant.CFragMini;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PFragMini;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.LogUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;


/**
 * @author CZB
 * @describe 矿机
 * @time 2019/4/20 16:21
 */
public class MiniMachineFragment extends BaseFragment implements CFragMini.IVFragMini {

    private final static int REQUEST_CODE = 0x00001;
    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.tv_record)
    TextView tvRecord;
    @BindView(R.id.rv_record)
    RecyclerView rvRecord;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;


    private MyMiniAdapter mAdapter;
    private CFragMini.IPFragMini mPresenter;
    private int page = 1, allDataSize;
    private List<MiniBean.ListBean> dataBeans;
    private String code;

    @Override
    protected void createPresenter() {
        mPresenter = new PFragMini(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mini_machine;
    }

    @Override
    protected void initView() {
        llTopLeft.setVisibility(View.GONE);
        tvTopTitle.setText(R.string.tab_2);
        tvTopRight.setText(R.string.shop);
        rvRecord.setLayoutManager(new LinearLayoutManager(mContext));
        onClickListener();
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        //商城
        tvTopRight.setOnClickListener(v -> {
            startActivityForResult(MiniShopActivity.class,REQUEST_CODE);
        });
        //确定开启
        btSure.setOnClickListener(v -> {
            code = etText.getText().toString().trim();
            if (TextUtils.isEmpty(code)) {
                toastNotifyShort(R.string.active_serial_hint);
                return;
            }
            mPresenter.pGetActive(BaseUrl.ACTIVE_MINI_URL, GetParamUtil.getActiveMiniParam(code));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.MY_MINI_MACHINE_URL, GetParamUtil.getMiniParam(page));
    }

    private void initAdapter() {
        LogUtils.d("执行了");
        mAdapter = new MyMiniAdapter(R.layout.item_my_mini, dataBeans);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvRecord.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if ("1".equals(mAdapter.getData().get(position).getState())) {
                mBundle = new Bundle();                mBundle.putString(SpMsg.INTENT_STR,mAdapter.getData().get(position).getId());
                startActivity(MiningActivity.class,mBundle);
            }
        });
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvRecord);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_CODE:
                page = 1;
                initData();
                break;
        }
    }

    @Override
    public void vGetInitSuccess(MiniBean dataBean) {
        dataBeans = dataBean.getList();
        allDataSize = dataBean.getTotal_num();//总条数
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(dataBeans);
            } else {
                if (dataBeans == null || dataBeans.size() == 0) {
                    tvRecord.setVisibility(View.GONE);
                } else {
                    tvRecord.setVisibility(View.VISIBLE);
                }
                initAdapter();
            }
        } else {
            if (dataBeans.size() != 0) {
                mAdapter.addData(dataBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void vGetActiveSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        etText.setText("");
        page = 1;
        initData();
    }
}
